﻿using Anonym.MVC.Models.Request;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Anonym.Common;
using Anonym.Models.Common;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using Anonym.Common.MyAttribute;
using Anonym.Service;
using Microsoft.Extensions.DependencyInjection;
using Anonym.Models.Objects;
using Anonym.Data.Entities;

namespace Anonym.MVC.Controllers
{
    public class AccountController : BaseController
    {
        public CmsService CmsService { get; set; }
        public AccountController(IServiceProvider p) : base(p)
        {
            CmsService = p.GetService<CmsService>();
        }

        #region 账号
        /// <summary>
        /// 账号管理列表
        /// </summary>
        /// <returns></returns>
        public IActionResult CmsUserIndex()
        {
            ViewBag.CmsUserState = Tools.EnumModelToSelectItem(typeof(EnumModel.CmsUserState));
            return View();
        }

        /// <summary>
        /// 账号管理列表数据
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        public string CmsUserIndexData(CommonRequest request)
        {
            #region JObject
            //JObject jobj = JObject.Parse(request.RequestContent);
            //var name = (string)jobj["Name"];
            //var id = jobj["Id"];
            #endregion

            var data = CmsService.GetCmsUserData(request.RequestContent.ToObject<Cms_UserInfo>());
            return data.Select(s => new
            {
                s.ID,
                s.EmployeeId,
                s.RealName,
                s.Name,
                s.Phone,
                s.State,
                StateName = ((EnumModel.CmsUserState)s.State).ToString(),
                s.CreateUserName,
                CreationTime = s.CreationTime.ToDateString(),
                s.GroupName,
            }).ToJson();
        }

        /// <summary>
        /// 编辑账号初始数据
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public string GetCmsUserDetail([FromBody] Cms_User user)
        {
            //这里以模型接收参数是为了演示，[FromBody]下必须用模型接收参数
            return CmsService.GetCmsUserDetail(user.ID).ToJson();
        }

        /// <summary>
        /// 编辑账号
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public string EditCmsUser()
        {
            long id = CmsBasicsInfo.DicRequest.GetDicByLong("id");

            string field = "id,Name,RealName,phone,password";
            if (id > 0) //修改
                field = "id,RealName,phone,state";

            CmsBasicsInfo.DicRequest.RetainPart(field);
            if (id <= 0)
            {
                CmsBasicsInfo.DicRequest.Add("CreatorUserId", CmsBasicsInfo.ID.ToString());
                CmsBasicsInfo.DicRequest.Add("CompanyId", CmsBasicsInfo.CompanyId.ToString());
            }

            return CmsService.EditCmsUser(CmsBasicsInfo.DicRequest).ToJson();
        }

        /// <summary>
        /// 角色授权页面
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public IActionResult AddUserGroupMap(long id)
        {
            ViewBag.EmployeeId = CmsBasicsInfo.EmployeeId;
            ViewBag.RealName = CmsBasicsInfo.RealName;
            return View();
        }

        /// <summary>
        /// 获取角色及账号是否关联信息
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public string GetUserGroupData()
        {
            string field = "Key,UserId";
            CmsBasicsInfo.DicRequest.RetainPart(field); //过滤参数，必须调用

            return CmsService.GetUserGroupData(CmsBasicsInfo.DicRequest).ToJson();
        }

        /// <summary>
        /// 角色授权
        /// </summary>
        /// <returns></returns>
        public string EditUserGroupMap()
        {
            //用户id，用户组id(逗号隔开)
            string field = "UserId,GroupIds";
            CmsBasicsInfo.DicRequest.RetainPart(field);
            return CmsService.EditUserGroupMap(CmsBasicsInfo.DicRequest).ToJson();
        }
        #endregion

        #region 角色
        /// <summary>
        /// 角色管理列表
        /// </summary>
        /// <returns></returns>
        public IActionResult CmsUserGroupIndex()
        {
            ViewBag.CmsUserState = Tools.EnumModelToSelectItem(typeof(EnumModel.CmsUserGroupState));
            return View();
        }

        /// <summary>
        /// 角色管理列表数据
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        public string CmsUserGroupIndexData()
        {
            //关键字，状态，当前页，每页条数
            string field = "Key,State,PageIndex,PageSize";
            CmsBasicsInfo.DicRequest.RetainPart(field);
            return CmsService.CmsUserGroupIndexData(CmsBasicsInfo.DicRequest).ToJson();
        }

        /// <summary>
        /// 编辑角色
        /// </summary>
        /// <returns></returns>
        public string EditCmsUserGroup()
        {
            //输入参数：id,名称
            string field = "id,name";   //不区分大小写
            CmsBasicsInfo.DicRequest.RetainPart(field);

            long id = CmsBasicsInfo.DicRequest.GetDicByLong("id");
            if (id <= 0)
            {
                CmsBasicsInfo.DicRequest.Add("CreatorUserId", CmsBasicsInfo.ID.ToString());
                CmsBasicsInfo.DicRequest.Add("CompanyId", CmsBasicsInfo.CompanyId.ToString());
            }

            return CmsService.EditCmsUserGroup(CmsBasicsInfo.DicRequest).ToJson();
        }

        /// <summary>
        /// 角色授权页面
        /// </summary>
        /// <returns></returns>
        public IActionResult CmsUserGroupAccredit()
        {
            //输入参数：id
            //string field = "id";

            return View();
        }

        /// <summary>
        /// 角色授权页面数据
        /// </summary>
        /// <returns></returns>
        public string CmsUserGroupAccreditData()
        {
            //输入参数：id
            long id = CmsBasicsInfo.DicRequest.GetDicByLong("id");
            return CmsService.CmsUserGroupAccreditData(id).ToJson();
        }

        /// <summary>
        /// 角色授权数据编辑
        /// </summary>
        /// <returns></returns>
        public string EditCmsUserGroupAccredit()
        {
            //用户id，用户组id(逗号隔开)
            string field = "groupId,menuIds";
            CmsBasicsInfo.DicRequest.RetainPart(field);
            return CmsService.EditCmsUserGroupAccredit(CmsBasicsInfo.DicRequest).ToJson();
        }
        #endregion
    }
}
